//
//  SimCommonCell.m
//  

#import "SimCommonCell.h"
#import "SimLineView.h"
#import "UIView+SimAdditions.h"
#import "SimDefine.h"
#import "SimBadgeView.h"

SimImpValue(kSimCellTitleFont, UIFont *)
SimImpValue(kSimCellDetailTitleFont, UIFont *)

SimImpValue(kSimCellTitleColor, UIColor *)
SimImpValue(kSimCellDetailColor, UIColor *)

SimImpValue(kSimCellAccessoryNormalName, NSString *)
SimImpValue(kSimCellAccessoryHighlightName, NSString *)

SimImpValue(kSimCellIpadOffset, NSNumber *)

@interface SimCommonCell()
{
    UITableViewCellStyle _cellStyle;
    SimLineView *_bottomLine;
    SimLineView *_topLine;
    SimBadgeView *_badgeView;
}

@end

@implementation SimCommonCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
	self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
	if (self) {
        _cellStyle = style;
        self.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor clearColor];
        if (!ABOVE_IOS7) {
            self.backgroundView = [[UIView alloc] init];
            self.selectionStyle = UITableViewCellSelectionStyleNone;
        }else{
            self.backgroundView = nil;
            self.selectionStyle = UITableViewCellSelectionStyleDefault;
        }

        self.accessoryViewLeft = 0;
        
        self.imageCustomFrame = CGRectZero;
        self.textLbCustomFrame = CGRectZero;
        self.detailLbCustomFrame = CGRectZero;
        self.imageOffset = CGPointZero;
        self.textLbOffset = CGPointZero;
        self.detailLbOffset = CGPointZero;
        
        self.textLabel.backgroundColor = [UIColor clearColor];
        self.textLabel.font = SimValue(kSimCellTitleFont, [UIFont systemFontOfSize:16.0]);
        self.textLabel.textColor = SimValue(kSimCellTitleColor, HEXRGBCOLOR(0x666666));
        self.detailTextLabel.backgroundColor = [UIColor clearColor];
        self.detailTextLabel.font = SimValue(kSimCellDetailTitleFont, [UIFont systemFontOfSize:15.0]);;
        self.detailTextLabel.textColor =  SimValue(kSimCellDetailColor, HEXRGBCOLOR(0xaaaaaa));;
        
        _lineOffX = -1; //默认和textLabel对齐
        _enable = YES;
	}
    
	return self;
}

- (void)setCellBgColor:(UIColor *)cellBgColor
{
    if (_cellBgColor != cellBgColor) {
        _cellBgColor = cellBgColor;
        
        self.backgroundColor = _cellBgColor;
        self.backgroundView = [[UIView alloc] init];
        self.backgroundView.backgroundColor = _cellBgColor;
    }
}

- (void)setBShowBottomDivideLine:(BOOL)show
{
    _bShowBottomDivideLine = show;
    if (_bShowBottomDivideLine) {
        if (!_bottomLine) {
            _bottomLine = [[SimLineView alloc] initWithWidth:self.width];
            if (_divideLineColor) {
                _bottomLine.lineColor = _divideLineColor;
            }
            [self.contentView addSubview:_bottomLine];
        }
    }
    else{
        if (_bottomLine) {
            [_bottomLine removeFromSuperview];
            _bottomLine = nil;
        }
    }
}

- (void)setBShowTopDivideLine:(BOOL)show
{
    _bShowTopDivideLine = show;
    if (_bShowTopDivideLine) {
        if (!_topLine) {
            _topLine = [[SimLineView alloc] initWithWidth:self.width];
            if (_divideLineColor) {
                _topLine.lineColor = _divideLineColor;
            }
            [self.contentView addSubview:_topLine];
        }
    }
    else{
        if (_topLine) {
            [_topLine removeFromSuperview];
            _topLine = nil;
        }
    }
}

- (void)setDivideLineColor:(UIColor *)color
{
    if (_divideLineColor != color) {
        _divideLineColor = color;
        if (_topLine) {
            _topLine.lineColor = color;
        }
        if (_bottomLine) {
            _bottomLine.lineColor = color;
        }
    }
}

- (void)drawRect:(CGRect)rect {
    if (self.cellBgColor != nil) {
        CGContextRef context = UIGraphicsGetCurrentContext();
        if (self.cellBgColor != nil) {
            [self.cellBgColor set];
            CGContextFillRect(context, rect);
        }
    }
    else{
        [super drawRect:rect];
    }
}

/*
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    [super setHighlighted:highlighted animated:animated];
    UIColor *highlightColor = kBasicBgColor;
    if (highlighted) {
        self.contentView.backgroundColor = highlightColor;
    }else{
        self.contentView.backgroundColor = [UIColor clearColor];
    }
}
*/

- (void)layoutSubviews{
    [super layoutSubviews];
    
    self.textLabel.backgroundColor = [UIColor clearColor];
    self.detailTextLabel.backgroundColor = [UIColor clearColor];
    self.selectedBackgroundView.frame = self.bounds;
    
    BOOL isIpad = DEVICE_IS_IPAD;
    NSInteger ipadOffset = [SimValue(kSimCellIpadOffset, @(15)) integerValue];
    if (self.accessoryViewLeft > 0) {
        self.accessoryView.left = self.accessoryViewLeft;
    }
    else{
        if (isIpad) {
            self.accessoryView.right = self.width- ipadOffset;
        }
    }
    
    //ImageView
    if (!CGRectEqualToRect(self.imageCustomFrame, CGRectZero)) {
        self.imageView.frame = [self frameBaseFrame:self.imageView.frame customFrame:self.imageCustomFrame];
    }
    else if(!CGPointEqualToPoint(self.imageOffset, CGPointZero)){
        self.imageCustomFrame = [self frameBaseFrame:self.imageView.frame transformOffset:self.imageOffset];
        self.imageView.frame = [self frameBaseFrame:self.imageView.frame customFrame:self.imageCustomFrame];
    }
    else{
        if (self.imageView.image && isIpad) {
            self.imageView.left = ipadOffset;
        }
    }
    
    //TextLabel
    if (!CGRectEqualToRect(self.textLbCustomFrame, CGRectZero)) {
        self.textLabel.frame =  [self frameBaseFrame:self.textLabel.frame customFrame:self.textLbCustomFrame];
    }
    else if(!CGPointEqualToPoint(self.textLbOffset, CGPointZero)){
        self.textLbCustomFrame = [self frameBaseFrame:self.textLabel.frame transformOffset:self.textLbOffset];
        self.textLabel.frame = [self frameBaseFrame:self.textLabel.frame customFrame:self.textLbCustomFrame];
    }
    else{
        if (isIpad) {
            if (self.imageView.image) {
                self.textLabel.left = self.imageView.right + 10;
            }
            else{
                self.textLabel.left = ipadOffset;
            }
        }
    }
    
    //DetailLable
    if (!CGRectEqualToRect(self.detailLbCustomFrame, CGRectZero)) {
        self.detailTextLabel.frame =  [self frameBaseFrame:self.detailTextLabel.frame customFrame:self.detailLbCustomFrame];
    }
    else if(!CGPointEqualToPoint(self.detailLbOffset, CGPointZero)){
        self.detailLbCustomFrame = [self frameBaseFrame:self.detailTextLabel.frame transformOffset:self.detailLbOffset];
        self.detailTextLabel.frame = [self frameBaseFrame:self.detailTextLabel.frame customFrame:self.detailLbCustomFrame];
    }
    else{
        if (isIpad) {
            if (_cellStyle == UITableViewCellStyleValue1) {
                if (self.accessoryView) {
                    self.detailTextLabel.left = self.accessoryView.left - self.detailTextLabel.width - ipadOffset;
                }
                else{
                    self.detailTextLabel.left = self.width - self.detailTextLabel.width - ipadOffset;
                }
            }
            else if(_cellStyle == UITableViewCellStyleSubtitle){
                self.detailTextLabel.left = self.textLabel.left;
            }
        }
    }
    
    if (_bottomLine != nil) {
        [_bottomLine.superview bringSubviewToFront:_bottomLine];
        _bottomLine.width = self.width;
        _bottomLine.bottom = self.height;
        _bottomLine.left = _lineOffX >= 0 ? _lineOffX : self.textLabel.frame.origin.x;
    }
    
    if (_topLine != nil) {
        _topLine.left = _lineOffX >= 0 ? _lineOffX : self.textLabel.frame.origin.x;
    }
    
    if (_showBadge) {
        if (!_badgeView) {
            _badgeView = [SimBadgeView pointItemWithSize:CGSizeMake(6, 6)];
            [self.contentView addSubview:_badgeView];
        }
        _badgeView.center = CGPointMake(self.textLabel.right+6, self.textLabel.top);
    }
    else{
        if (_badgeView) {
            [_badgeView removeFromSuperview];
            _badgeView = nil;
        }
    }
    
    if (_cellStyle == UITableViewCellStyleValue1 && self.detailTextLabel.text.length > 0) {
        CGFloat right = self.detailTextLabel.right;
        [self.detailTextLabel sizeToFit];
        self.detailTextLabel.right = right;
        self.detailTextLabel.centerY = self.textLabel.centerY;
        
        if (self.textLabel.right > self.detailTextLabel.left-5) {
            self.textLabel.width = self.detailTextLabel.left - self.textLabel.left-5;
        }
    }
}

- (CGRect)frameBaseFrame:(CGRect)frame customFrame:(CGRect)customFrame
{
    if (customFrame.origin.x > 0) {
        frame.origin.x = customFrame.origin.x;
    }
    if (customFrame.origin.y > 0) {
        frame.origin.y = customFrame.origin.y;
    }
    if (customFrame.size.width > 0) {
        frame.size.width = customFrame.size.width;
    }
    if (customFrame.size.height > 0) {
        frame.size.height = customFrame.size.height;
    }
    return frame;
}

- (CGRect)frameBaseFrame:(CGRect)frame transformOffset:(CGPoint)point
{
    CGRect newFrame;
    newFrame.origin.x = frame.origin.x + point.x;
    newFrame.origin.y = frame.origin.y + point.y;
    return newFrame;
}

- (void)showCommonAccessoryView:(BOOL)show{
    if (show) {
        UIImage *normalImage = [UIImage imageNamed:(SimValue(kSimCellAccessoryNormalName, @"icon_tree_accessory"))];
        UIImage *highlightImage = [UIImage imageNamed:(SimValue(kSimCellAccessoryHighlightName, @"icon_tree_accessory_high"))];
        UIImageView* next = [[UIImageView alloc] initWithImage:normalImage highlightedImage:highlightImage];
		self.accessoryView = next;
    }
    else{
        self.accessoryView = nil;
    }
}

- (void)setEnable:(BOOL)enable{
    self.userInteractionEnabled = enable;
    _enable = enable;
    if(enable){
        if (ABOVE_IOS7) {
            self.selectionStyle = UITableViewCellSelectionStyleDefault;
        }
        else{
            self.selectionStyle = UITableViewCellSelectionStyleBlue;
        }
    }
    else{
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
}

- (void)setEnableSelectEffect:(BOOL)enable
{
    if (enable && ABOVE_IOS7) {
        self.selectionStyle = UITableViewCellSelectionStyleDefault;
    }
    else{
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
}

@end
